package tech.aistar;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

@WebListener
public class PathListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //项目的上下文路径
        String path = servletContext.getContextPath();

        //放入到application作用域
        servletContext.setAttribute("path",path);

        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
             //返回IP地址
            servletContext.setAttribute("path2",address.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
